package cn.mothin.frame.core.base;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.LogFactory;

import cn.mothin.frame.core.interfaces.system.IConfig;

public abstract class BaseConfig implements IConfig {
    protected String id;
    protected String caption;
    protected Object value;
    protected boolean allowConfig;
    protected String memo;
    protected int ordinal = Integer.MAX_VALUE;

    public BaseConfig(String id, String caption, Object value,boolean allowConfig) {
        this.id = id;
        this.caption = caption;
        this.value = value;
        this.allowConfig=allowConfig;
    }

    public BaseConfig(String id, String caption, Object value, String memo,boolean allowConfig) {
        this.id = id;
        this.caption = caption;
        this.value = value;
        this.memo = memo;
        this.allowConfig=allowConfig;
    }

    public BaseConfig(String id, String caption, Object value, String memo,
            int ordinal,boolean allowConfig) {
        this.id = id;
        this.caption = caption;
        this.value = value;
        this.memo = memo;
        this.ordinal = ordinal;
        this.allowConfig=allowConfig;
    }

    public static List<IConfig> getConfigList(Class<?> c) {
        List<IConfig> list = new ArrayList<IConfig>();
        for (Field field : c.getFields()) {
            if (field.getType() == c && Modifier.isStatic(field.getModifiers())
                    && Modifier.isPublic(field.getModifiers())) {
                try {
                    list.add((IConfig) field.get(null));
                } catch (Exception e) {
                    LogFactory.getLog(c).error(e);
                }
            }
        }
        return list;
    }

    public String getId() {
        return id;
    }

    public String getCaption() {
        return caption;
    }

    public Object getValue() {
        return value;
    }

    public boolean isAllowConfig() {
        return allowConfig;
    }

    public String getMemo() {
        return memo;
    }

    public int getOrdinal() {
        return ordinal;
    }
}
